<?php
namespace Home\Controller;
use Think\Controller;
class CartController extends Controller 
{
    public function add() {
    	if(IS_POST)
    	{var_dump($_POST);exit;
    		$model = D('Cart');
    		if($model->create(I('post.'), 1))
    		{
    			if($id = $model->add())
    			{
    				$this->success('添加成功！', U('lst?p='.I('get.p')));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}

		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '添加',
			'_page_btn_name' => '列表',
			'_page_btn_link' => U('lst'),
		));
		$this->display();
    }
    public function edit()
    {
    	$id = I('get.id');
    	if(IS_POST)
    	{
    		$model = D('Cart');
    		if($model->create(I('post.'), 2))
    		{
    			if($model->save() !== FALSE)
    			{
    				$this->success('修改成功！', U('lst', array('p' => I('get.p', 1))));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
    	$model = M('Cart');
    	$data = $model->find($id);
    	$this->assign('data', $data);

		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '修改',
			'_page_btn_name' => '列表',
			'_page_btn_link' => U('lst'),
		));
		$this->display();
    }
    public function del()
    {
    	$model = D('Home/Cart');
    	if($model->delete(I('get.id', 0)) !== FALSE)
    	{
    		$this->success('删除成功！', U('lst', array('p' => I('get.p', 1))));
    		exit;
    	}
    	else 
    	{
    		$this->error($model->getError());
    	}
    }
    public function lst()
    {
    	$model = D('Home/Cart');
    	$data = $model->cartList();
    	$this->assign(array(
    	    'data' => $data,
            '_page_title' => '购物车列表',
            '_page_keywords' => '购物车列表',
            '_page_description' => '购物车列表',
    	));

    	$this->display();
    }

    public function ajaxCartList() {
        $cartModel = D('cart');
        $data = $cartModel->cartList();
        echo json_encode($data);
    }

    //ajax修改购物车
    public function update_cart(){

        if (IS_POST || true){
            //var_dump(I("post."));exit;
            $cartModel = D('Cart');
            $cartModel->create();
            $goods_number = $cartModel->updateGoodsNumber(I('post.goods_number'));
            if($goods_number){
                $this->success('数量更新成功!');
                exit;
            }
            $this->error('更新失败!,原因:'.$cartModel->getError());
        }
    }
}